package com.dhcc.core.modules.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.datascope.DataScope;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.dao.DeptMapper;
import com.dhcc.core.modules.system.dao.UserMapper;
import com.dhcc.core.modules.system.entity.Dept;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.core.modules.system.service.IDeptService;
import com.dhcc.core.modules.system.vo.ExportDeptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 资源服务
 *
 * @ClassName: DeptServiceImpl
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午10:39:33
 */
@Service
public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    DeptMapper deptMapper;

    @Override
    @Transactional
    public void delDept(Long deptId) {

        Wrapper<User> u_wrapper = new EntityWrapper<>();
        u_wrapper = u_wrapper.eq("dept_id", deptId);
        List<User> u_ls = this.userMapper.selectList(u_wrapper);
        if (u_ls == null || u_ls.size() > 0) {
            throw new BizException("部门下有用户存在!");
        }

        Wrapper<Dept> d_wrapper = new EntityWrapper<>();
        d_wrapper = d_wrapper.like("path", "%[" + deptId + "]%");
        List<Dept> d_ls = this.deptMapper.selectList(d_wrapper);
        if (d_ls == null || d_ls.size() > 0) {
            throw new BizException("部门下有子部门存在!");
        }
        // 删除部门
        this.deptMapper.deleteById(deptId);
    }

    @Override
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.baseMapper.tree();
        tree.add(ZTreeNode.createRoot());
        return tree;
    }

    @Override
    public List<ZTreeNode> tree(DataScope dataScope) {
        List<ZTreeNode> tree = this.baseMapper.tree(dataScope);
        //tree.add(ZTreeNode.createRoot());
        return tree;
    }
    @Override
    public List<Dept> list(Long parentId,String condition) {
        return this.baseMapper.list(parentId,condition);
    }

    @Override
    public List<Dept> list(DataScope dataScope,Long parentId,String condition) {
        return this.baseMapper.list(dataScope,parentId,condition);
    }

    @Override
    public List<Dept> listV1(String code) {
        return this.baseMapper.listV1(code);
    }

    @Override
    public Dept selectByCode(String code) {
        Dept entity = new Dept();
        entity.setCode(code);
        return this.baseMapper.selectOne(entity);
    }

    @Override
    public Boolean hasDeptByCode(String code) {
        Wrapper<Dept> wrapper = new EntityWrapper<>();
        wrapper.eq("code", code);
        Integer count = this.baseMapper.selectCount(wrapper);
        return count>0;
    }
    @Override
    public List<ExportDeptVo> exportDepts(String condition) {
        return this.baseMapper.exportDepts(condition);
    }

    @Override
    @Transactional
    public void importDepts(List<List<Object>> all, Long parentId) {
        Dept parentDept = this.baseMapper.selectById(parentId);
        for (int i = 1; i < all.size(); i++) {
            List<Object> ls = all.get(i);
            String deptCode = ls.get(0).toString();
            String deptName = ls.get(1).toString();
            String deptFullname = ls.get(2).toString();
            String hisCode = ls.get(3).toString();
            String deptType = ls.get(4).toString();
            if ("医院".equals(deptType)) {
                deptType = "A";
            } else if ("院区".equals(deptType)) {
                deptType = "H";
            }else if ("科室".equals(deptType)) {
                deptType = "E";
            }else if ("病区".equals(deptType)) {
                deptType = "W";
            } else {
                deptType = "E";
            }
            String description = ls.get(5).toString();

            // 判断科室是否重复
            Dept dept = new Dept();
            dept.setCode(deptCode);
            dept.setParentId(parentId);
            dept = this.baseMapper.selectOne(dept);
            if (CommonUtil.isEmpty(dept)) {
                dept = new Dept();
                dept.setName(deptName);
                dept.setFullname(deptFullname);
                dept.setCode(deptCode);
                dept.setHisCode(hisCode);
                dept.setDeptType(deptType);
                dept.setParentId(parentId);
                dept.setDescription(description);
                dept.setWeight(i);
                dept.setLevels(parentDept.getLevels()+1);
                dept.insertAllColumn();
            } else {
                dept.setName(deptName);
                dept.setFullname(deptFullname);
                dept.setCode(deptCode);
                dept.setHisCode(hisCode);
                dept.setDeptType(deptType);
                dept.setParentId(parentId);
                dept.setDescription(description);
                dept.setWeight(i);
                dept.setLevels(parentDept.getLevels()+1);
                dept.updateById();
            }
        }
    }

    @Override
    public int syncDept(Long parentId) {
        //获取当前选中节点信息
        Dept parentDept = this.baseMapper.selectById(parentId);
        //获取当前医院/院区的下属住院科室
        List<Map<String, Object>> deptList = this.baseMapper.locationList(parentDept.getHisCode());
        for (int i = 0; i < deptList.size(); i++) {
            Map<String, Object> deptMap = deptList.get(i);
            String hisCode = deptMap.get("hisCode").toString();
            String deptName = deptMap.get("deptName").toString();
            // 判断科室是否重复
            Dept dept = new Dept();
            dept.setCode(hisCode);
            dept.setParentId(parentId);
            dept = this.baseMapper.selectOne(dept);
            if (CommonUtil.isEmpty(dept)) {
                dept = new Dept();
                dept.setName(deptName);
                dept.setFullname(deptName);
                dept.setCode(hisCode);
                dept.setHisCode(hisCode);
                dept.setDeptType("E");
                dept.setParentId(parentId);
                dept.setDescription(deptName);
                dept.setWeight(i);
                dept.setLevels(parentDept.getLevels()+1);
                dept.insertAllColumn();
            } else {
                dept.setName(deptName);
                dept.setFullname(deptName);
                dept.setCode(hisCode);
                dept.setHisCode(hisCode);
                dept.setDeptType("E");
                dept.setParentId(parentId);
                dept.setDescription(deptName);
                dept.setWeight(i);
                dept.setLevels(parentDept.getLevels()+1);
                dept.updateById();
            }
        }
        return deptList.size();
    }
}
